<?php
/**
 * Created by PhpStorm.
 * User: 17732
 * Date: 2018/6/19
 * Time: 22:06
 */

namespace app\admin\controller;


use app\admin\model\Books;
use app\admin\model\Borrow;
use app\admin\model\User;
use think\Controller;
use think\Session;

class Index extends BaseController
{
    /**
     * 首页
     */
    public function index(){
        // 检测是否登陆
        if (!$this->checkIndex()){
            $this->redirect('/index/index/login');
        }
        return view();
    }

    /**
     * 搜索  type 1--只搜人名，2--只搜书名，3--都搜
     */
    public function search(){
        $page = !empty($_POST['page'])?$_POST['page']: 1;
        $name = $_POST['name'];
        $type = $_POST['type'];
        // 1--会员管理
        if($type == 1){
            $query = User::searchUser($name);
            $query = Publicmethod::pageing($query,$page,9);
            if(empty($query)){
                return 0;  // 无此人
            }
            else{
                return $query;
            }
        }
        // 2--借阅管理
        if($type == 2){
            $pageType = $_POST['pageType'];   // 类型
            if($pageType == 1){
                $query1 = Borrow::borrow([1]);   // 状态为 1 的集合
                $states = [3];
                $query = Borrow::borrowSearch($states,$name);
                // 当状态为 1 的集合中的uid，在状态为 3 的集合中存在同样的 uid，那么说明这个人借了书，没有同意这个按钮
                foreach ($query as $k3 => $v3){
                    foreach ($query1 as $k1 => $v1){
                        if($v1['uid'] == $v3['uid']){
                            $v3['expect'] = 1;         // 预约状态：1--代表正在借书中， 0--没有借书进行申请
                            $query[$k3] = $v3;
                        }
                    }
                }

            }
            else if($pageType == 2){
                $states = [6];
                $query = Borrow::borrowSearch($states,$name);
                $query = Publicmethod::isBeOverdueAll($query);
            }
            // 分页
            $query = Publicmethod::pageing($query,$page,3);
            $query['type'] = $pageType;
            return $query;
        }
        // 3--图书记录
        if($type == 3){
            $pageType = $_POST['pageType'];   // 类型
            // 借阅记录
            if($pageType == 1){
                $states = [1,2,5,7];
                $query = Borrow::borrowSearch($states,$name);
                $query = Publicmethod::isBeOverdueAll($query);
            }
            // 逾期记录
            else if($pageType == 2){
                $states = [1,7];
                $res = Borrow::borrowSearch($states,$name);
                // 检查是否逾期
                $query = Publicmethod::isBeOverdue($res);
            }
            // 操作记录
            else if($pageType == 3){
                $query = Books::selectAllBooksSearch($name);
            }
            if(empty($query)){
                return 0;
            }

            $query = Publicmethod::pageing($query,$page,3);
            $query['type'] = $pageType;
            return $query;
        }
        // 4--首页搜索
        if($type == 4){
            $states = [1];
            $query = Borrow::borrowSearch($states,$name);
            $query = Publicmethod::pageing($query,$page);
            if(empty($query)){
                $query = Books::statesEqOneSearch($name);
                $query = Publicmethod::pageing($query,$page,4);
                if(empty($query)){
                    return 0;
                }
                else{
                    return $query;
                }
            }
            else{
                return $query;
            }
        }
    }

    /**
     * 退出登录
     */
    public function signOut(){
        Session::clear();
        $this->redirect('/index/index/index', 302);
    }


}